//	AUTHOR:			ENRICO CANTONI
//	CREATED:		SEPTEMBER 21, 2017
//	MODIFIED: 		NOVEMBER 7, 2022
//	DESCRIPTION:	Creates the event-study graphs
//					(Figures 3, 5, A5, A6, A7, A8, and A9).


// Set Stata version
version 16.1

********************************************************************************
********************************************************************************

// Outcomes
local Y_1			voted
local Y_2			voted
local Y_3			voted
local Y_4			voted
local Y_5			voted

// Samples
local s_1 			s_kids
local s_2 			s_all
local s_3			s_all
local s_4			s_kids
local s_5			s_kids

// Regressors of interest
local X_1			anykids_0to18
local X_2 			married
local X_3			cohabiting
local X_4			anykids_0to18
local X_5			anykids_0to18

// Interactions
local int_1			female
local int_2 		female
local int_3			female
local int_4			female
local int_5			female

// Covariates
#delimit ;
local covs
	year##nbhd
	precinct_age
	precinct_citizen
	precinct_female
	precinct_hh_income_mod
	hh_citizen
	hh_income_mod
	income
	irpef
;
#delimit cr
local covs_kids 	anykids_0to5 anykids_6to11 anykids_12to17 anykids_18more
local covs_marital	maritalstatus
local covs_1 		`covs' female##`covs_marital'
local covs_2 		`covs' female##(`covs_kids')
local covs_3		`covs' female##(`covs_kids')
local covs_4 		`covs' female##`covs_marital'
local covs_5 		`covs' female##`covs_marital'

// Graph names
local graph_1		Figure_5
local graph_2 		Figure_3
local graph_3		Figure_A9
local graph_4		Figure_A8
local graph_5		Figure_A7

// X-axis titles
local xtitle_1 		"Election Relative to First Election w/ Children"
local xtitle_2 		"Election Relative to Marriage"
local xtitle_3 		"Election Relative to Cohabitation"
local xtitle_4 		"Election Relative to First Election w/ Children"
local xtitle_5 		"Election Relative to First Election w/ Children"

// Y-axis labels
local ylbl_1		"-.04(.02).04"
local ylbl_2		"-.04(.02).04"
local ylbl_3		"-.08(.02).08"
local ylbl_4		"-.10(.02).10"
local ylbl_5		"-.10(.02).10"

// Y-axis titles
local ytitle_1		"Voter Turnout (Coefficient)"
local ytitle_2		"Voter Turnout (Coefficient)"
local ytitle_3		"Voter Turnout (Coefficient)"
local ytitle_4		"Voter Turnout (Coefficient)"
local ytitle_5		"Voter Turnout (Coefficient)"

********************************************************************************
********************************************************************************

*** EVENT-STUDY GRAPHS ***

forvalues g = 1/5 {

	// Load data for right sample
	use ${analysis_clean} if `s_`g''

	// For cohabitation, we need to create the treatment var
	if "`X_`g''" == "cohabiting" {

		// Flag cohabiting individuals
		#delimit ;
		gegen temp_cohabiting = total(hh_position == "Cohabiting":hh_position),
			by(ID_hh year)
		;
		bysort ID_hh year (hh_position):
			gen temp_check =
				!married[1] & !married[2] &						// Not married
				hh_position[2] == "Cohabiting":hh_position &	// Cohabiting
				female[1] != female[2]							// Diff sexes
		;
		gen cohabiting =	temp_cohabiting == 1 &
							temp_check & 
							inlist(hh_position,
								"Head of household":hh_position,
								"Cohabiting":hh_position)
		;
		#delimit cr
		drop temp_cohabiting temp_check
	}

	// Set panel structure
	bysort ID_voter (year): gen counter = _n
	xtset ID_voter counter

	// First election w/ treatment
	gen temp = counter if `X_`g'' & !L.`X_`g''
	egen t0 = min(temp), by(ID_voter)
	drop temp

	// Election relative to first election w/ treatment
	gen t = (counter - t0)

	// Initialize matrix of results
	local n_cols = 16
	#delimit ;
	local colnames "t b_m cimin_m cimax_m
		b_d cimin_d cimax_d
		b_f cimin_f cimax_f
		mrgn_b_m mrgn_cimin_m mrgn_cimax_m
		mrgn_b_f mrgn_cimin_f mrgn_cimax_f"
	;
	#delimit cr
	mat results = J(1, `n_cols', 0)
	matrix colnames results = `colnames'
	local col_b = 2
	local col_cimin	= 3
	local col_cimax	= 4
	local col_mrgn_b = 11
	local col_mrgn_cimin = 12
	local col_mrgn_cimax = 13

	// Initialize empty row
	mat row = J(1, `n_cols', 0)

	// Populate matrix of results w/ relative time counter
	sum t
	local base = -1 - r(min)
	local t = r(min)
	local tmin = r(min)
	while `t' <= r(max) {
		if `t' == -1 {
			local ++t
		}
		mat row[1,1] = `t'
		mat results = results \ row
		local ++t
	}

	// Sample restrictions for heterogeneous children's effects
	// by whether a person returns to work after birth or not
	if `g' == 4 {
		gegen temp = max(income < .7*L.income & t == 0), by(ID_voter)
		drop if temp
	}
	if `g' == 5 {
		gegen temp = max(income >= .7*L.income & t == 0), by(ID_voter)
		drop if temp
	}

	// Rescale relative time fixed effects
	replace t = t - `tmin'
	mvencode t, mv(99)

	// Run regression
	#delimit ;
	reghdfe `Y_`g'' `covs_`g'' `int_`g''##ib`base'.t,
		absorb(age#female ID_voter) cluster(ID_hh ID_voter)
	;
	#delimit cr

	// Store estimates
	levelsof t if !inlist(t, `base', 99), local(vals)
	local row = 2
	foreach t of local vals {

		// Store estimates for men
		local v `t'.t
		mat results[`row', `col_b'] = _b[`v']
		#delimit ;
		mat results[`row', `col_cimin'] = _b[`v'] -
			invttail(e(df_r),0.025)*_se[`v']
		;
		mat results[`row', `col_cimax'] = _b[`v'] +
			invttail(e(df_r),0.025)*_se[`v']
		;
		#delimit cr

		// Store interaction estimates
		local i 1.`int_`g''#`t'.t
		mat results[`row', `col_b'+3] = _b[`i']
		#delimit ;
		mat results[`row', `col_cimin'+3] = _b[`i'] -
			invttail(e(df_r),0.025)*_se[`i']
		;
		mat results[`row', `col_cimax'+3] = _b[`i'] +
			invttail(e(df_r),0.025)*_se[`i']
		;
		#delimit cr
		
		// Store estimates for women
		lincom `v' + `i'
		mat results[`row', `col_b'+6] = r(estimate)
		#delimit ;
		mat results[`row', `col_cimin'+6] = r(estimate) -
			invttail(r(df),0.025)*r(se)
		;
		mat results[`row', `col_cimax'+6] = r(estimate) +
			invttail(r(df),0.025)*r(se)
		;
		#delimit cr

		// Increase row counter
		local ++row
	}

	// Store outcome means in last pre-event election, separately by gender
	sum `Y_`g'' if !`int_`g'' & t == `base'
	local mn_m = r(mean)
	sum `Y_`g'' if `int_`g'' & t == `base'
	local mn_f = r(mean)

	// Transform matrix of results to data
	clear
	svmat results, names(col)
	replace t = -1 if _n == 1

	// Reshape long to separate stats for males, females, and f-m diff
	cd ${output_graphs}
	#delimit ;
	reshape long b_ cimin_ cimax_ mrgn_b_ mrgn_cimin_ mrgn_cimax_,
		i(t) j(temp) string
	;
	#delimit cr
	replace t = t - .05 if temp == "f"
	replace t = t + .05 if temp == "m"

	// Redefine marginal outcomes
	foreach v of varlist b_ cimin_ cimax_ {
		replace mrgn_`v' = `v' + `mn_f' if temp == "f"
		replace mrgn_`v' = `v' + `mn_m' if temp == "m"
	}

	// Make graph for two genders
	#delimit ;
	twoway 	(rcap cimin cimax t if temp == "f", lcolor(orange))
			(rcap cimin cimax t if temp == "m", lcolor(navy))
			(connected b t if temp == "f",
				lcolor(none) lpattern(dash) mcolor(orange) sort(t))
			(connected b t if temp == "m",
				lcolor(none) lpattern(dash) mcolor(navy) sort(t)),
			graphregion(fcolor(white) lcolor(white) margin(zero))
			legend(off)
			xline(0, lcolor(gs14) lpattern(solid) lwidth(15))
			xtitle(`xtitle_`g'')
			ylabel(`ylbl_`g'', nogrid)
			yline(0, lcolor(gs9) lpattern(shortdash))
			ytitle(`ytitle_`g'')
			saving(`graph_`g''_a.gph, replace)
	;
	#delimit cr
	graph export `graph_`g''_a.pdf, replace

	// Make graph for f-m difference
	#delimit ;
	twoway	(rcap cimin cimax t if temp == "d", lcolor(cranberry))
			(connected b t if temp == "d",
				lcolor(cranberry) lpatter(solid) mcolor(cranberry) sort(t)),
			graphregion(fcolor(white) lcolor(white) margin(zero))
			legend(off)
			xline(0, lcolor(gs14) lpattern(solid) lwidth(15))
			xtitle(`xtitle_`g'')
			ylabel(`ylbl_`g'', nogrid)
			yline(0, lcolor(gs9) lpattern(shortdash))
			ytitle("Female-Male Difference (Coefficient)")
			saving(`graph_`g''_b.gph, replace)
	;
	#delimit cr
	graph export `graph_`g''_b.pdf, replace

	// Make graph for marginal effects
	if inrange(`g', 1, 2) {
		if `g' == 1 {
			local graph_`g'	Figure_A6
		}
		else if `g' == 2 {
			local graph_`g'	Figure_A5
		}
		#delimit ;
		twoway 	(rcap mrgn_cimin mrgn_cimax t if temp == "f", lcolor(orange))
				(rcap mrgn_cimin mrgn_cimax t if temp == "m", lcolor(navy))
				(connected mrgn_b t if temp == "f",
					lcolor(none) lpattern(dash) mcolor(orange) sort(t))
				(connected mrgn_b t if temp == "m",
					lcolor(none) lpattern(dash) mcolor(navy) sort(t)),
				graphregion(fcolor(white) lcolor(white) margin(zero))
				legend(off)
				xline(0, lcolor(gs14) lpattern(solid) lwidth(15))
				xtitle(`xtitle_`g'')
				ylabel(, nogrid)
				ytitle("Voter Turnout (Predicted)")
				saving(`graph_`g''.gph, replace)
		;
		#delimit cr
		graph export `graph_`g''.pdf, replace
	}
	clear
}
